package com.quanyan.travel.web;

import com.quanyan.api.APIResponse;
import com.quanyan.travel.api.req.*;
import com.quanyan.travel.api.resp.RespRemindMsg;
import com.quanyan.travel.service.TravelOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/travel/order/")
public class OrderController {

    @Autowired
    private TravelOrderService travelOrderService;

    private final static Logger logger = LoggerFactory.getLogger(OrderController.class);


    /**
     * 开启支付方式
     *
     * */
    @RequestMapping(value="/openPayType", method = RequestMethod.POST)
    public APIResponse<?> openPayType(@RequestParam("ids") List<Integer> ids)  {
        if (null == ids) {
            ids.add(1);
            ids.add(2);
        }
        if (travelOrderService.openPayType(ids)) {
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail("开通失败");
    }

    /**
     * 评论送鸟蛋
     *
     * */
    @RequestMapping(value="/assetsRuleAdd", method = RequestMethod.POST)
    public APIResponse<?> assetsRuleAdd(@RequestParam("ids") List<Integer> ids)  {

        if (travelOrderService.openPayType(ids)) {
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail("开通失败");
    }


    /**
     * 用户报名
     *
     * */
    @RequestMapping(value="/userSign", method = RequestMethod.POST)
    public APIResponse<?> userSign(@RequestBody @Valid ReqSignUser reqSignUser, BindingResult bindingResult, HttpServletRequest request)  {
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(bindingResult.getFieldError().getDefaultMessage());
        }
        return travelOrderService.userSign((Integer) request.getAttribute("uid"), reqSignUser);
    }


    /**
     * 用户预约下单
     *
     * */
    @RequestMapping(value="/userOrder", method = RequestMethod.POST)
    public APIResponse<?> userOrder(@RequestBody @Valid ReqOrderMsg reqOrderMsg, BindingResult bindingResult, HttpServletRequest request) throws Exception {
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(bindingResult.getFieldError().getDefaultMessage());
        }
        return travelOrderService.userOrder((Integer) request.getAttribute("uid"), reqOrderMsg, request);
    }

    /**
     * 用户下单
     *
     * */
    @RequestMapping(value="/confirmOrder", method = RequestMethod.POST)
    public APIResponse<?> confirmOrder(@RequestBody @Valid ReqOrderConfirmMsg reqOrderConfirmMsg, BindingResult bindingResult, HttpServletRequest request) throws UnsupportedEncodingException {
        if(bindingResult.hasErrors()){
            return APIResponse.returnFail(bindingResult.getFieldError().getDefaultMessage());
        }
        return travelOrderService.confirmOrder((Integer) request.getAttribute("uid"), reqOrderConfirmMsg, request);
    }


    /**
     * 用户下单成功返回信息
     *
     * */
    @RequestMapping(value="/paySuccess", method = RequestMethod.POST)
    public APIResponse<?> paySuccess(@RequestBody @Valid ReqOrderConfirmMsg reqOrderConfirmMsg, HttpServletRequest request)  {
        return travelOrderService.paySuccess((Integer) request.getAttribute("uid"), reqOrderConfirmMsg.getOrderNo(), request);
    }


    /**
     * 用户取消订单
     *
     * */
    @RequestMapping(value="/orderCancel", method = RequestMethod.POST)
    public APIResponse<?> orderCancel(@RequestBody @Valid ReqOrderConfirmMsg reqOrderConfirmMsg, HttpServletRequest request)  {
        return travelOrderService.cancelOrder((Integer) request.getAttribute("uid"), reqOrderConfirmMsg);
    }


    /**
     * 用户退款预览
     *
     * */
    @RequestMapping(value="/orderRefundPreview", method = RequestMethod.POST)
    public APIResponse<?> orderRefundPreview(@RequestBody  Map<String, Object> map, HttpServletRequest request) throws Exception {
        if (null == map.get("orderNo")) {
            return APIResponse.returnFail("参数不合法");
        }
        return travelOrderService.preRefundOrder(map.get("orderNo")+"");
    }

    /**
     * 用户退款
     *
     * */
    @RequestMapping(value="/orderRefund", method = RequestMethod.POST)
    public APIResponse<?> orderRefund(@RequestBody  Map<String, Object> map, HttpServletRequest request) throws Exception {
        if (null == map.get("orderNo")) {
            return APIResponse.returnFail("参数不合法");
        }
        return travelOrderService.refundOrder((Integer) request.getAttribute("uid"), map.get("orderNo")+"", map.get("refundReason")+"");
    }

    /**
     * 订单列表
     *
     * */
    @RequestMapping(value="/orderList", method = RequestMethod.POST)
    public APIResponse<?> orderList(@RequestBody ReqPaging reqPaging, HttpServletRequest request)  {
        return APIResponse.returnSuccess(travelOrderService.orderList((Integer) request.getAttribute("uid"), reqPaging.getPageNo(), reqPaging.getPageSize()));
    }
    /**
     * 订单详情
     *
     * */
    @RequestMapping(value="/orderDetail", method = RequestMethod.POST)
    public APIResponse<?> orderDetail(@RequestBody ReqOrderConfirmMsg reqOrderConfirmMsg,BindingResult bindingResult, HttpServletRequest request)  {
        if(null == reqOrderConfirmMsg.getOrderNo()){
            return APIResponse.returnFail("orderNo不能为空");
        }
        return travelOrderService.orderDetail((Integer) request.getAttribute("uid"), reqOrderConfirmMsg.getOrderNo());
    }

    /**
     * 活动结束以后去评价
     *
     * */
    @RequestMapping(value="/assess", method = RequestMethod.POST)
    public APIResponse<?> assess(@RequestBody @Valid ReqAccess reqAccess, HttpServletRequest request)  {
        return travelOrderService.access((Integer) request.getAttribute("uid"), reqAccess);
    }

    /**
     * 需要提醒的信息
     *
     * */
    @RequestMapping(value="/remindMsg", method = RequestMethod.POST)
    public APIResponse<RespRemindMsg> remindMsg(HttpServletRequest request)  {
        RespRemindMsg respRemindMsg = new RespRemindMsg();
        respRemindMsg.setRemindOrderList(travelOrderService.remindOrderNoList((Integer) request.getAttribute("uid")));
        return APIResponse.returnSuccess(respRemindMsg);
    }



}
